//
//  JZBAddFooter.m
//  JZB
//
//  Created by liujianlong on 2022/8/31.
//

#import "JZBAddFooter.h"
#import "JZBAddImgCell.h"

@interface JZBAddFooter()
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

@end
@implementation JZBAddFooter
- (void)awakeFromNib{
    [super awakeFromNib];
    [self.collectionView registerNib:[JZBAddImgCell nib] forCellWithReuseIdentifier:[JZBAddImgCell ID]];
}
- (void)setImgDatas:(NSArray *)imgDatas{
    _imgDatas = imgDatas;
    int row = (SCREEN_WIDTH - 40) / 106;
    int coulom = ceil((self.imgDatas.count + 1) * 1.0 / row);
    self.H = 20 + 96 * coulom + 59;
    [self.collectionView reloadData];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  self.imgDatas.count + 1;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    JZBAddImgCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[JZBAddImgCell ID] forIndexPath:indexPath];
    if (indexPath.row == self.imgDatas.count) {
        cell.img.image = [UIImage imageNamed:@"添加"];
    }else{
        cell.img.image = [UIImage imageWithData:self.imgDatas[indexPath.row]];
    }
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.imgDatas.count) {
        if (self.addImgBlock) {
            self.addImgBlock();
        }
    }
    
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake(106,96);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 20, 20, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
@end
